Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement custom localization #1784

Merged
merged 3 commits into from
Feb 29, 2024
Merged

implement custom localization #1784

merged 3 commits into from
Feb 29, 2024

Conversation

Masterjun3
Copy link
Collaborator

@Masterjun3 Masterjun3 commented Feb 28, 2024

This PR needs to be checked properly.

Resolves #908 .

It implements 3 user settings to customize the date format, the time format, and the decimal number format (see images below).

Implementation Details

I build a custom CultureInfo, and then, in every single request overwrite CultureInfo.CurrentCulture as soon as possible. By using Middleware.

Middleware Location

Where to place the Middleware? We actually use the UseRequestLocalization() middleware to let .NET select the best culture initially. So I thought immediately afterwards would be a good place to place my overwrite middleware. But it turns out I need the User object, so UseAuthentication has to run first. That's why I placed it directly after that.

Caching

Caching is critical. We don't want to read the DB on every request just for the locale. The first idea was to just cache each user's CultureInfo. This alone would work, but there are two Problems:

  1. On every restart the cache is cleared, and each user will do one additional DB call to get their CultureInfo build, again and again.
  2. Most users don't use custom cultures, so the cache will be filled with non-modified objects.

That's why I decided to use a second cache. This second cache caches the HashSet of the users that actually use custom locales.

The HashSet cache and the user's own CultureInfo cache is cleared when they change their Profile Settings.

Hot Path

  • For logged out visitors: The Middleware checks whether they are logged in. They aren't, so it is skipped.
  • For non-custom-locale users: After confirming the login, it loads the cached HashSet of custom locale users, and they aren't in there. So it is skipped.
  • For custom-locale users: After loading the cached HashSet and finding the user, it loads the cached CultureInfo and overwrites the CurrentCulture. Then it is done.

image

@vadosnaprimer
Copy link
Collaborator

vadosnaprimer commented Feb 29, 2024

I tested with a few language settings in the browser and it works well with obeying and overriding them. I'd only wish the settings to be near Time Zone settings in the GUI and not between avatar and signature.

@adelikat adelikat merged commit 80ee0e4 into main Feb 29, 2024
1 check passed
@adelikat adelikat deleted the custom-localization branch February 29, 2024 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Customizable locale settings
3 participants